const cookieparser = process.server ? require('cookieparser') : undefined

export const state = () => {
  return {
    jwtStr: null,
    userInfo:null
  }
}
export const mutations = {
  setjwtStr (state, jwtStr) {
    state.jwtStr = jwtStr
  },
  setuserInfo (state, userInfo) {
    state.userInfo = userInfo
  }
}
export const actions = {
  nuxtServerInit ({ commit }, { req }) {
    let jwtStr = null
    if (req.headers.cookie) {
      const parsed = cookieparser.parse(req.headers.cookie)
      try {
        jwtStr = JSON.parse(parsed.jwtStr)
      } catch (err) {
        // No valid cookie found
      }
    }
    commit('setjwtStr', jwtStr)
  }
}
